package com.zy.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zy.entity.EmailUser;
import com.zy.mapper.EmailUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;

@Controller
public class ZyEmailController {
    @Autowired
    EmailUserMapper emailUserMapper;

    /**
     * @Author: 杨深俊
     * @Method: getEmails
     * @Date: 2021/4/7
     * @Time: 18:57
     * 查询所有邮件
     * pageNum 当前页数
     * pageSize 每页记录条数
     */
    @RequestMapping("/appEmailManage")
    public ModelAndView getEmails(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                  @RequestParam(value = "pageSize", defaultValue = "7") Integer pageSize,
                                  @RequestParam(value = "queryName", required = false) String queryName,
                                  @RequestParam(value = "queryPhone", required = false) String queryPhone,
                                  @RequestParam(value = "queryDate", required = false) String queryDate) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("AppEmailManage");


        IPage<EmailUser> emailIPage = new Page<>(pageNum, pageSize);

        HashMap map = new HashMap();
        System.out.println("queryName " + queryName);
        if (!StringUtils.isEmpty(queryName)) {
            map.put("queryName", queryName);
        } else {
            queryName = "";
        }
        if (!StringUtils.isEmpty(queryPhone)) {
            map.put("queryPhone", queryPhone);
        } else {
            queryPhone = "";
        }
        if (!StringUtils.isEmpty(queryDate)) {
            map.put("queryDate", queryDate);
        } else {
            queryDate = "";
        }


        IPage<EmailUser> emails = emailUserMapper.getEmails(emailIPage, map);

        modelAndView.addObject("queryName", queryName);
        modelAndView.addObject("queryPhone", queryPhone);
        modelAndView.addObject("queryDate", queryDate);
        modelAndView.addObject("emails", emails);
        return modelAndView;
    }

    //通过id删除邮件
    @RequestMapping("/delEmail")
    public String delEmailById(Integer id) {
        emailUserMapper.deleteEmail(id);
        return "redirect:/appEmailManage";
    }
}
